package com.wyl.template.util;

import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;

/**
 * @Auther: wyl
 * @Date: 2021/7/5
 * @Description:
 */
public class Des3New {
    private static final String DESkey = "amljZW5nZ2FveHVleWFndWFubGliYW5nb25nc2hp";
    private static final byte[] keyiv = {1, 2, 3, 4, 8, 7, 6, 5};

    /**
     * ECB加密,不要IV
     *
     * @param key  密钥
     * @param data 明文
     * @return Base64编码的密文
     * @throws Exception
     */
    private static byte[] des3EncodeECB(byte[] key, byte[] data)
            throws Exception {
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, deskey);
        byte[] bOut = cipher.doFinal(data);
        return bOut;
    }

    /**
     * ECB解密,不要IV
     *
     * @param key  密钥
     * @param data Base64编码的密文
     * @return 明文
     * @throws Exception
     */
    public static byte[] ees3DecodeECB(byte[] key, byte[] data)
            throws Exception {
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, deskey);
        byte[] bOut = cipher.doFinal(data);
        return bOut;
    }

    /**
     * CBC加密
     *
     * @param key   密钥
     * @param keyiv IV
     * @param data  明文
     * @return Base64编码的密文
     * @throws Exception
     */
    private static byte[] des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data)
            throws Exception {
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(keyiv);
        cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
        byte[] bOut = cipher.doFinal(data);
        return bOut;
    }

    /**
     * CBC解密
     *
     * @param key   密钥
     * @param keyiv IV
     * @param data  Base64编码的密文
     * @return 明文
     * @throws Exception
     */
    private static byte[] des3DecodeCBC(byte[] key, byte[] keyiv, byte[] data)
            throws Exception {
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(keyiv);
        cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
        byte[] bOut = cipher.doFinal(data);
        return bOut;
    }

    /**
     * DES3加密算法
     *
     * @param str 待加密字符串
     * @return 加密后字符串结果
     * @throws Exception
     */
    public static String encodeECB(String str) throws Exception {
        byte[] key = new BASE64Decoder().decodeBuffer(DESkey);
        byte[] data = str.getBytes("UTF-8");
        byte[] strEncode = des3EncodeECB(key, data);

        return Base64.encodeBase64String(strEncode);
    }

    /**
     * DES3解密算法
     *
     * @param str 待解密字符串
     * @return 解密后字符串结果
     * @throws Exception
     */
    public static String decodeECB(String str) throws Exception {
        byte[] key = new BASE64Decoder().decodeBuffer(DESkey);
        byte[] data = new BASE64Decoder().decodeBuffer(str);
        byte[] strDecode = ees3DecodeECB(key, data);

        return new String(strDecode, "UTF-8");
    }

    /**
     * DES3加密算法
     *
     * @param str 待加密字符串
     * @return 加密后字符串结果
     * @throws Exception
     */
    public static String encodeCBC(String str) throws Exception {
        byte[] key = new BASE64Decoder().decodeBuffer(DESkey);
        byte[] data = str.getBytes("UTF-8");
        byte[] strEncode = des3EncodeCBC(key, keyiv, data);

        return Base64.encodeBase64String(strEncode);
    }

    /**
     * DES3解密算法
     *
     * @param str 待解密字符串
     * @return 解密后字符串结果
     * @throws Exception
     */
    public static String decodeCBC(String str) throws Exception {
        byte[] key = new BASE64Decoder().decodeBuffer(DESkey);
        byte[] data = new BASE64Decoder().decodeBuffer(str);
        byte[] strDecode = des3DecodeCBC(key, keyiv, data);

        return new String(strDecode, "UTF-8");
    }

    public static void main(String[] args) throws Exception {
        String str = "110101197";
//        String strCBC_encode = Des3New.encodeCBC(str).replaceAll("[\\s*\t\n\r]", "");
        String strCBC_encode = Des3New.encodeCBC(str);
        String strCBC_decode = Des3New.decodeCBC(strCBC_encode);
        System.out.println(strCBC_encode);
        System.out.println(strCBC_decode);

//        7mbmXtHJNr+DaK5Q/tSvX2JxKdLyq/eC
//        110101197010211321
    }
}
